package com.lb.jwitter.hibernate;

import org.apache.commons.logging.Log;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import com.lb.utils.orm.hibernate.HibernateGenericDao;
import org.springframework.transaction.annotation.Transactional;
import com.lb.jwitter.Tbl_QLDTTXA_HocVien_DonXinXetMienGiam;

@Repository("tbl_QLDTTXA_HocVien_DonXinXetMienGiamDAO")
public class Tbl_QLDTTXA_HocVien_DonXinXetMienGiamHibernateDAO extends HibernateGenericDao<Tbl_QLDTTXA_HocVien_DonXinXetMienGiam, Long> implements Tbl_QLDTTXA_HocVien_DonXinXetMienGiamDAO {
   private Log log = LogFactory.getLog(Tbl_QLDTTXA_HocVien_DonXinXetMienGiamHibernateDAO.class);

   @Autowired
   public Tbl_QLDTTXA_HocVien_DonXinXetMienGiamHibernateDAO(@Qualifier("sessionFactory")   SessionFactory sessionFactory) {
       super(sessionFactory);
   }

   @SuppressWarnings("unchecked")
@Transactional
   public final List<Tbl_QLDTTXA_HocVien_DonXinXetMienGiam> findAll(int pageSize, int page) {
       if( pageSize == 0) return getSession().createCriteria(Tbl_QLDTTXA_HocVien_DonXinXetMienGiam.class).list(); 
       return getSession().createCriteria(Tbl_QLDTTXA_HocVien_DonXinXetMienGiam.class).setFirstResult(pageSize * (page-1)).setMaxResults(pageSize).list();
   }
   
@Transactional
   public final Long save(Tbl_QLDTTXA_HocVien_DonXinXetMienGiam _newInstance) {
   	return super.save(_newInstance);
   }

//@Transactional
//   public final boolean exists(Long id) {
//       boolean result = (Integer) getSession().createCriteria(Tbl_QLDTTXA_HocVien_DonXinXetMienGiam.class).setProjection(Projections.rowCount()).add(
//                       Restrictions.eq("id", id)).uniqueResult() == 1;
//       log.info("Result: "+result);
//   	return result;
//   }

@Transactional
   public Collection<Tbl_QLDTTXA_HocVien_DonXinXetMienGiam> find(Tbl_QLDTTXA_HocVien_DonXinXetMienGiam obj,int pageSize,int page){
   	Collection<Tbl_QLDTTXA_HocVien_DonXinXetMienGiam> _tbl_QLDTTXA_HocVien_DonXinXetMienGiams = new ArrayList<Tbl_QLDTTXA_HocVien_DonXinXetMienGiam>();
   	try {
   		String sql = "select d from tbl_QLDTTXA_HocVien_DonXinXetMienGiam as d where 1=1 ";
   sql += " and d.id = " + obj.getId() + " ";
   sql += " and d.hocVienId. = " + obj.getHocVienId().getId() + " ";
   if (obj.getNgayNop() != null)
   {
       DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
       sql += " and d.ngayNop = '" + df.format(obj.getNgayNop()) + "' ";
   }
   sql += " and d.tongSoMon = " + obj.getTongSoMon() + " ";
   sql += " and d.TronGoi = " + obj.getTronGoi() + " ";
   sql += " and d.TongTienDong = " + obj.getTongTienDong() + " ";
       if(obj.getSoBienLai() != null && !obj.getSoBienLai().equalsIgnoreCase("")){
   		sql +=  " and d.soBienLai like '%" +obj.getSoBienLai() + "%' " ;
   }
       if(obj.getNguoiThu() != null && !obj.getNguoiThu().equalsIgnoreCase("")){
   		sql +=  " and d.nguoiThu like '%" +obj.getNguoiThu() + "%' " ;
   }
   		sql += " order by d.id DESC ";
   		_tbl_QLDTTXA_HocVien_DonXinXetMienGiams = getSession().createQuery(sql).setFirstResult(pageSize*(page-1)).setMaxResults(pageSize).list();
   	} catch (Exception e) {
   		e.printStackTrace();
   	}
   	return _tbl_QLDTTXA_HocVien_DonXinXetMienGiams;
   }

}//endclass
